//
//  SearchViewController.m
//  BaseAPP
//
//  Created by Evisu on 16/12/10.
//  Copyright © 2016年 evisum. All rights reserved.
//

#import "SearchViewController.h"
#import "DMSearchBar.h"
#import "WeChatViewController.h"

@interface SearchViewController ()<UISearchBarDelegate>

//搜索栏底下的背景View
@property (nonatomic, strong)UIView *bgView;

//返回按钮
@property (nonatomic, strong)UIButton *backBtn;

//搜索栏
@property (nonatomic, strong)DMSearchBar *searchBar;

@end

@implementation SearchViewController

- (instancetype)init {

    if (self = [super init]) {
        
        [self initViews];
        [self layoutViews];
    }
    
    return self;
}

- (void)initViews {

    self.view.backgroundColor = KBACKGROUND_COLOR;
    
    //背景
    self.bgView = [[UIView alloc] init];
    self.bgView.backgroundColor = HEXColor(@"EFEEF4");
    
    //返回
    self.backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backBtn setBackgroundImage:[UIImage imageNamed:@"fts_search_backicon_ios7"] forState:UIControlStateNormal];
    [self.backBtn addTarget:self action:@selector(didClickedBackBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    //搜索栏
    self.searchBar = [[DMSearchBar alloc] init];
    self.searchBar.delegate = self;

    [self.view addSubview:self.bgView];
    [self.bgView addSubview:self.backBtn];
    [self.view addSubview:self.searchBar];
}

- (void)layoutViews {

    self.bgView.sd_layout
    .topSpaceToView(self.view, 20)
    .leftEqualToView(self.view)
    .widthIs(40)
    .heightIs(44);
    
    self.backBtn.sd_layout
    .centerXEqualToView(self.bgView)
    .centerYEqualToView(self.bgView)
    .widthIs(10)
    .heightIs(18);
    
    self.searchBar.sd_layout
    .topSpaceToView(self.view, 20)
    .leftSpaceToView(self.bgView, -10)
    .rightEqualToView(self.view)
    .heightIs(44);

}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    
    [self.searchBar becomeFirstResponder];
}

/**
 *  返回
 */
- (void)didClickedBackBtn:(UIButton *)btn {

    [self.bgView removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {

    //显示取消按钮
    self.searchBar.showsCancelButton = YES;
    UIButton *cancleButton = [self.searchBar valueForKey:@"cancelButton"];
    [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancleButton setTitleColor:[UIColor colorWithRed:28.9/255.0 green:187.0/255.0 blue:3.5/255.0 alpha:1.0] forState:UIControlStateNormal];
    cancleButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
    
    return YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

    [self.searchBar resignFirstResponder];
    
    [self.navigationController popViewControllerAnimated:YES];
    
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

- (void)setType:(NSInteger)type {

    _type = type;
    
    if (type == 0) {
        
        self.searchBar.placeholder = @"搜索朋友圈";
        [self.searchBar setImage:[UIImage imageNamed:@"fts_edu_sns_icon"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
        return;
    }
    
    if (type == 1) {
        
        self.searchBar.placeholder = @"搜索文章";
        [self.searchBar setImage:[UIImage imageNamed:@"fts_searchicon_article"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
        return;
    }
    
    if (type == 2) {
        
        self.searchBar.placeholder = @"搜索公众号";
        [self.searchBar setImage:[UIImage imageNamed:@"fts_searchicon_brandcontact"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
        return;
    }
}

@end
